package usi.ch.onto.lucene.analysis;

import javatools.parsers.PlingStemmer;
import org.apache.lucene.analysis.TokenFilter;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.TermAttribute;

import java.io.IOException;

public class PlingStemFilter extends TokenFilter {

    private final TermAttribute termAtt;

    public PlingStemFilter(TokenStream in) {
        super(in);
        termAtt = addAttribute(TermAttribute.class);
    }

    @Override
    public final boolean incrementToken() throws IOException {
        if (!input.incrementToken())
            return false;

        final String term = termAtt.term();
        final String s = PlingStemmer.stem(term);
        if (s != null && !s.equals(term))
            termAtt.setTermBuffer(s);

        return true;
    }

}
